<template>
  <div class="dev-form-plain-wrapper">
    <div class="dev-form-redbox">
      <!-- 选择评估对象类型 -->
      <div class="dev-section">
        <div class="dev-section-title">选择评估对象类型</div>
        <a-checkbox
          v-model:checked="checkAll"
          @change="onCheckAllChange"
          style="margin-right: 24px;margin-bottom: 20px;"
        >全选</a-checkbox>
        <a-checkbox-group
          class="dev-checkbox-group"
          :options="objectTypeOptions"
          v-model:value="checkedTypes"
          @change="onCheckedTypesChange"
        >
        <template #label="{ objType }">
          <span >{{ objType }}</span>
        </template>
      </a-checkbox-group>
      </div>
      <!-- 选择评估范围 -->
      <div class="dev-section">
        <div class="dev-section-title">选择评估范围</div>
        <a-checkbox class="dev-checkbox">整个数据源评估</a-checkbox>
        <div class="dev-transfer-box">
          <a-transfer
            v-model:target-keys="targetKeys"
            :data-source="mapping"
            :titles="['可选项', '已选项']"
            :render="item => item.operateResources"
             @change="handleChange"
            :list-style="{ width: '400px', height: '400px' }"
          />
        </div>
      </div>
      <!-- 高级配置 -->
      <div class="dev-section dev-advanced">
        <div class="dev-section-title" style="display: flex; align-items: center;">
          <span>高级配置</span>
          <a-button
            type="link"
            size="small"
            @click="showAdvanced = !showAdvanced"
            style="margin-left: 8px;"
          >
            <component :is="showAdvanced ? UpOutlined : DownOutlined" />
          </a-button>
        </div>
        <div v-show="showAdvanced">
          <div class="dev-adv-row">
            <span>采集并发：</span>
            <a-input-number :min="1" :max="10" v-model:value="detailConfig.thread" style="width: 80px;" />
          </div>
          <div class="dev-adv-row">
            <span>采集时间：</span>
            <a-radio-group v-model:value="detailConfig.timeType">
              <a-radio value="0">所有时间段</a-radio>
              <a-radio value="1">指定时间段</a-radio>
            </a-radio-group>
            <template v-if="detailConfig.timeType === '1'">
              <a-date-picker
                v-model:value="detailConfig.startTime"
                placeholder="选择开始时间"
                style="margin-left: 12px; width: 160px;"
                show-time
              />
              <a-date-picker
                v-model:value="detailConfig.endTime"
                placeholder="选择结束时间"
                style="margin-left: 8px; width: 160px;"
                show-time
              />
            </template>
          </div>
          <div class="dev-adv-row">
            <span>探测敏感信息：</span>
            <a-radio-group v-model:value="detailConfig.sensitiveType">
              <a-radio value="1">是</a-radio>
              <a-radio value="0">否</a-radio>
            </a-radio-group>
            <template v-if="detailConfig.sensitiveType === '1'">
              <span style="margin-left: 12px;">探测行数：</span>
              <a-input-number
                v-model:value="detailConfig.rows"
                :min="1"
                :max="1000"
                style="width: 80px;"
              />
            </template>
          </div>
        </div>
      </div>
    </div>
  </div>
  <div class="flex justify-center">
        <a-button type="primary" @click="prev" style="margin-right: 10px;">上一步</a-button>
        <a-button type="primary" @click="next" :disabled="!checkedTypes.length || !targetKeys.length">下一步</a-button>
       </div>
</template>

<script setup>
import { ref,defineEmits,onMounted, reactive } from 'vue';
import { Checkbox, CheckboxGroup, Transfer, Button, InputNumber, RadioGroup, Radio, DatePicker ,message} from 'ant-design-vue';
import { UpOutlined, DownOutlined } from '@ant-design/icons-vue';
import * as http from '@/api/flow/createTask';
import { useStore } from 'vuex';
const store = useStore();
const objectTypeOptions = ref([])
const checkedTypes = ref([]);
const baseConfig = ref({})
const checkAll = ref(true);
const detailConfig = reactive({
  thread:1,
  timeType:'0',
  startTime:null,
  endTime:null,
  sensitiveType:'1',
  rows:10
})
const onCheckAllChange = (e) => {
  objectTypeOptions.value.forEach(item => {
    item.isSelect = e.target.checked ? 1 : 0
  })
  let isSelect = objectTypeOptions.value.every(item => item.isSelect === 1)
  if(isSelect){
    checkedTypes.value = objectTypeOptions.value.map(item => item.value)
  }else{
    checkedTypes.value = []
  }
}
const onCheckedTypesChange = (val) => {
  objectTypeOptions.value.forEach(item => {
    if(val.includes(item.value)){
      item.isSelect = 1
    }else{
      item.isSelect = 0
    }
  })
  let isSelect = objectTypeOptions.value.every(item => item.isSelect === 1)
  checkAll.value = isSelect; 
}

const mapping = ref([])
const targetKeys = ref([]);
const handleChange = (targetKeys, direction, moveKeys) => {
  console.log(targetKeys, direction, moveKeys);
  targetKeys.value = targetKeys
  mapping.value.forEach(item => {
    if(targetKeys.includes(item.operateResources)){
      item.isSelect = 1
    }else{
      item.isSelect = 0
    }
  })
};
const showAdvanced = ref(true);

const next =async () => {
  let res = await saveConfig()
  emit('next')
  return
  if(res.code === '000000'){
    emit('next')
  }else{
    message.error(res.message)
  }
}
const prev = () => {
  emit('prev')
}
const emit = defineEmits(['next','prev'])

// 保存
const saveConfig = async () => {
  let dataJson ={
      objType:objectTypeOptions.value,
      mapping:mapping.value,
      detailConfig,
    }
  let params = {
    flowId:store.state.taskInfo.flowId,
    dataJson:JSON.stringify(dataJson)
  }
  return await http.saveConfig(params)
}
// 查询获取数据
const queryConfig = async () => {
  let params = {
    flowId:store.state.taskInfo.flowId,
  }
  let res = await http.queryConfig(params)
  try {
    if(res.code === '000000'){
      mapping.value = res.data.mapping

      objectTypeOptions.value = res.data.objType
      objectTypeOptions.value.forEach(item => {
        item.value = item.id
      })
      checkAll.value = objectTypeOptions.value.every(item => item.isSelect === 1)
      mapping.value.forEach(item => {
        item.key = item.operateResources
        if(item.isSelect === 1){
          targetKeys.value.push(item.operateResources)
        }
      })
    }
  } catch (error) {
    console.log(error)
  }
}
onMounted(() => {
  queryConfig()
})
</script>

<style scoped>
.dev-form-plain-wrapper {
  width: 100%;
  min-height: 480px;
  padding: 40px 0 24px 0;
  display: flex;
  justify-content: center;
}
.dev-form-redbox {
}
.dev-section {
  margin-bottom: 32px;
}
.dev-section-title {
  font-weight: 600;
  font-size: 16px;
  margin-bottom: 16px;
}
.dev-checkbox-group {
  display: flex;
  flex-wrap: wrap;
  gap: 24px 32px;
  margin-bottom: 8px;
}
.dev-checkbox {
  margin-bottom: 12px;
}
.dev-transfer-box {
  display: flex;
  align-items: flex-start;
  gap: 16px;
  margin-top: 8px;
}
.dev-advanced {
  padding-top: 18px;
}
.dev-adv-row {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 14px;
}
@media (max-width: 900px) {
  .dev-form-redbox {
    min-width: 0;
    max-width: 100vw;
    padding: 18px 4vw 8px 4vw;
  }
  .dev-form-plain-wrapper {
    justify-content: center;
  }
}
</style>